package com.eshop.user.controller;

import com.eshop.base.model.Order;
import com.eshop.base.model.User;
import com.eshop.base.protocol.Result;
import com.eshop.base.service.IOrderService;
import com.eshop.base.service.IUserService;
import com.eshop.base.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * <p>
 * 用户核心表 前端控制器
 * </p>
 *
 * @author Your Name
 * @since 2025-05-12
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;
    @DubboReference
    private IOrderService orderService;
    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/order/{id}")
    public Result<Order> order(@PathVariable("id") String id) {
        Result<Order> result;
        Order order = userService.order(id);
        log.info("order:{}", order);
        result = Result.success(order);
        return result;
    }

    @RequestMapping("/create/{username}")
    public Result<User> create(@PathVariable("username") String username) {
        User user = userService.create(username);
        return Result.success(user);
    }

    @RequestMapping("/trans/{id}")
    public Result<String> trans(@PathVariable("id") String id) {
        userService.trans(id);
        return Result.success();
    }

    @RequestMapping("/dubbo")
    public Result<String> dubbo() {
        String result = userService.dubbo();
        return Result.success(result);
    }

    @GlobalTransactional
    @RequestMapping("/trans")
    public Result<String> trans() {
        Order order = orderService.transCreate(RandomUtil.getRandomString(10));
        User user = userService.transCreate(RandomUtil.getRandomString(10));
        return Result.success("result");
    }
}
